﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InformationSystem.BLL.Core.Model;
using InformationSystem.BLL.Core.Service;
using Ext.Net;

namespace InformationSystem.WebSite.Module.NoticeMgr
{
    public partial class GetMessage : BasePage
    {
        protected LoginUser user;
        public IInformationService InformationService { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            user = (LoginUser)Session["LoginUser"];
            lastlogintime.InnerText = string.Format("最后登录时间:{0}", user.LastLoginDateTime.ToString("yyyy/MM/dd HH:mm"));
            if (!X.IsAjaxRequest && !IsPostBack)
            {
                if (Request.QueryString["m"] == null) return;
                string m = Request.QueryString["m"].ToLower();
                int id = 0;
                if (Request.QueryString["id"] == null) return;
                if (!int.TryParse(Request.QueryString["id"], out id)) return;

                switch (m)
                {
                    case "affiche":
                        Affiche affiche = InformationService.GetAffiche(id, user);
                        if (affiche == null) return;
                        //TODO更加严格的权限会判断是否有当前用户的信息存在内
                        InformationService.UpdateAfficheUserReadStatus(id, user);
                        this.subjectlocation.InnerText = affiche.Title;
                        this.contentlocation.InnerHtml = affiche.Content;
                        this.commitUser.InnerText = string.Format("发布者:{0}", affiche.CommitUserName);
                        this.keylocation.InnerText = string.Format("关键词: {0}", affiche.KeyWord);
                        hdntype.Value = "affiche";
                        hdnid.Value = id.ToString();
                        if (affiche.AttachmentList == null || affiche.AttachmentList.Count == 0)
                            attachmentlocation.Hidden = true;
                        ObjHolder1.Items.Add("Attachment", affiche.AttachmentList);
                        relylocation.Listeners.ClearListeners();
                        break;
                    case "workinfo":
                        WorkInfo workinfo = InformationService.GetWorkInfo(id, user);
                        if (workinfo == null) return;
                        InformationService.UpdateWorkInfoUserReadStatus(id, user);
                        this.subjectlocation.InnerText = workinfo.Title;
                        this.contentlocation.InnerHtml = workinfo.Content;
                        this.commitUser.InnerText = string.Format("发布者:{0}", workinfo.CommitUserName);
                        this.keylocation.Visible = false;
                        hdntype.Value = "workinfo";
                        hdnid.Value = id.ToString();
                        if (workinfo.AttachmentList == null || workinfo.AttachmentList.Count == 0)
                            attachmentlocation.Hidden = true;
                        ObjHolder1.Items.Add("Attachment", workinfo.AttachmentList);
                        relylocation.Listeners.ClearListeners();
                        break;
                    case "msg":
                        Msg msg = InformationService.GetMsgDetailByID(id, user);
                        if (msg == null) return;
                        //更新阅读记录
                        if (!msg.HasRead)
                            InformationService.UpdateMsgUserReadStatusToRead(id, user.UserID);
                        this.subjectlocation.InnerText = msg.Subject;
                        this.contentlocation.InnerHtml = msg.Content;
                        this.commitUser.InnerText = string.Format("发布者:{0}", msg.SendUserName);
                        this.keylocation.InnerText = string.Format("接收人列表: {0}", msg.RecvUserNameList);
                        hdntype.Value = "msg";
                        hdnid.Value = id.ToString();
                        if (msg.AttachmentList == null || msg.AttachmentList.Count == 0)
                            attachmentlocation.Hidden = true;
                        ObjHolder1.Items.Add("Attachment", msg.AttachmentList);
                        IList<MsgReplyInfo> msgreplylist = InformationService.GetMsgReplyInfoList(id);
                        if (msgreplylist.Count == 0)
                            relylocation.Hidden = true;
                        ObjHolder1.Items.Add("Reply", msgreplylist);
                        break;
                }
                taskmgr.Tasks[0].Interval = (int)Application[Common.ClientRefreshSpan];
            }
        }

        protected override bool CommitPermissionCheck()
        {
            throw new NotImplementedException();
        }

        protected void Reply_Click(object sender, DirectEventArgs e)
        {
            if (hdntype.Value != "msg") return;
            int id = 0;
            if (Request.QueryString["id"] == null) return;
            if (!int.TryParse(Request.QueryString["id"], out id)) return;
            string reply = replycontent.Text;
            if (reply.Trim() == string.Empty) return;
            if (InformationService.ReplyMsg(id, user, reply))
            {
                IList<MsgReplyInfo> msgreplylist = InformationService.GetMsgReplyInfoList(id);
                if (msgreplylist.Count == 0)
                    relylocation.Hidden = true;
                else
                    relylocation.Hidden = false;
                ReplyTemplate.Overwrite(this.relylocation, msgreplylist);
            }

        }
    }
}